package com.docker.apps.active.vm;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MediatorLiveData;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.dcbfhd.utilcode.utils.ActivityUtils;
import com.dcbfhd.utilcode.utils.KeyboardUtils;
import com.dcbfhd.utilcode.utils.ToastUtils;
import com.docker.apps.R;
import com.docker.apps.active.api.ActiveService;
import com.docker.apps.active.vo.ActivePersionVo;
import com.docker.apps.order.vo.entity.InvoiceEntity;
import com.docker.common.common.router.AppRouter;
import com.docker.common.common.utils.ParamUtils;
import com.docker.common.common.utils.cache.CacheUtils;
import com.docker.common.common.vm.container.NitCommonContainerViewModel;
import com.docker.common.common.widget.XPopup.BottomPopup;
import com.docker.common.common.widget.XPopup.CenterEditPopup;
import com.docker.core.di.netmodule.ApiResponse;
import com.docker.core.di.netmodule.BaseResponse;
import com.docker.core.repository.NitBoundCallback;
import com.docker.core.repository.NitNetBoundObserver;
import com.docker.core.repository.Resource;
import com.google.gson.internal.LinkedTreeMap;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.XPopupCallback;

import java.util.HashMap;

import javax.inject.Inject;

public class ActivePersionListViewModel extends NitCommonContainerViewModel {


    @Inject
    ActiveService activeService;

    public String acctivityid;

    @Inject
    public ActivePersionListViewModel() {

    }

    @Override
    public LiveData<ApiResponse<BaseResponse>> getServicefun(String apiurl, HashMap param) {
        return activeService.persionList(param);
    }


    //api.php?m=activity.updateReviewStatus

    public final MediatorLiveData<String> mPersionStatusLv = new MediatorLiveData<>();

    public void updateReviewStatus(ActivePersionVo item, char flag, View view) {

        if ("1".equals(String.valueOf(flag))) { // 驳回
            CenterEditPopup centerEditPopup = new CenterEditPopup(ActivityUtils.getTopActivity());
            BasePopupView basePopupView = new XPopup.Builder(ActivityUtils.getTopActivity())
                    .setPopupCallback(new XPopupCallback() {
                        @Override
                        public void onCreated() {
                            KeyboardUtils.showSoftInput(view);
                            EditText editText = centerEditPopup.findViewById(R.id.ed_content);
                            editText.setSelection(editText.getText().toString().trim().length());
                            centerEditPopup.findViewById(R.id.tv_sure).setOnClickListener(v -> {
                                if (!TextUtils.isEmpty(editText.getText().toString().trim())) {
                                    centerEditPopup.dismiss();
                                    processCommand(item, flag, editText.getText().toString().trim());
                                } else {
                                    ToastUtils.showShort("请输入驳回原因");
                                }
                            });
                            centerEditPopup.findViewById(R.id.tv_cancel).setOnClickListener(v -> {
                                centerEditPopup.dismiss();
                            });
                        }

                        @Override
                        public void beforeShow() {

                        }

                        @Override
                        public void onShow() {


                        }

                        @Override
                        public void onDismiss() {
                            KeyboardUtils.hideSoftInput(view);
                        }

                        @Override
                        public boolean onBackPressed() {
                            return false;
                        }
                    }).asCustom(centerEditPopup);

            basePopupView.show();

        } else {

            processCommand(item, flag, "");
        }

    }


    public void lookReason(ActivePersionVo item, View view) {

        CenterEditPopup centerEditPopup = new CenterEditPopup(ActivityUtils.getTopActivity());
        BasePopupView basePopupView = new XPopup.Builder(ActivityUtils.getTopActivity())
                .setPopupCallback(new XPopupCallback() {
                    @Override
                    public void onCreated() {
                        ((TextView) centerEditPopup.findViewById(R.id.tv_title)).setText("查看驳回原因");
                        EditText editText = centerEditPopup.findViewById(R.id.ed_content);
                        editText.setText(item.refuse);
                        editText.setEnabled(false);
                        editText.setFocusable(false);
                        editText.setFocusableInTouchMode(false);
                        centerEditPopup.findViewById(R.id.tv_cancel).setVisibility(View.GONE);
                        centerEditPopup.findViewById(R.id.tv_sure).setVisibility(View.GONE);
                    }

                    @Override
                    public void beforeShow() {

                    }

                    @Override
                    public void onShow() {


                    }

                    @Override
                    public void onDismiss() {
                    }

                    @Override
                    public boolean onBackPressed() {
                        return false;
                    }
                }).asCustom(centerEditPopup);

        basePopupView.show();
    }


    private void processCommand(ActivePersionVo item, char flag, String refuse) {
        HashMap<String, String> parm = new HashMap<>();
        parm.put("joinid", item.joinid);
        if ("1".equals(String.valueOf(flag))) {
            parm.put("status", "-1");
            parm.put("refuse", refuse);
        } else {
            parm.put("status", "1");
        }
        mPersionStatusLv.addSource(RequestServer(activeService.updateReviewStatus(parm)), new NitNetBoundObserver<String>(new NitBoundCallback<String>() {
            @Override
            public void onComplete(Resource<String> resource) {
                super.onComplete(resource);
                mPersionStatusLv.setValue(parm.get("status"));
                ToastUtils.showShort("操作成功");
                if ("1".equals(String.valueOf(flag))) {
                    item.status = 1;
                    mItems.remove(item);
                } else {
                    item.status = 2;
                }
            }
        }));
    }

    public void enterVerfity(ActivePersionVo item, View view) {
        ARouter.getInstance()
                .build(AppRouter.ACTIVE_MANAGE_VERFIC)
                .withString("activityid", acctivityid).navigation();

    }
}
